#include "CAppStateGame.h"
#include "CAppStateManager.h"

CAppStateGame CAppStateGame::Instance;

CAppStateGame::CAppStateGame() {
    Surf_BG = NULL;
    Surf_Ball = NULL;
    CField::Field.Live = false;
}

void CAppStateGame::OnMouseMove(int mX, int mY, int relX, int relY, bool Left,bool Right,bool Middle) {
    CField::Field.ClearFocus();
    int x = mX / 40; int y = (mY - 40) / 40;
    if (CField::Field.OkField(x, y) && !CField::Field.Animate)
        CField::Field.FocusBall(x, y);
}

void CAppStateGame::OnLButtonDown(int mX, int mY) {
    int x = mX / 40; int y = (mY - 40) / 40;
    if (CField::Field.OkField(x, y) && !CField::Field.Animate)
        if (CField::Field.SelectBall(x, y))
            CSoundBank::SoundControl.Play(CAppStateGame::CubeBoom);
}

void CAppStateGame::OnKeyDown(SDLKey sym, SDLMod mod, Uint16 unicode) {
    if (sym == SDLK_ESCAPE)
        CAppStateManager::SetActiveAppState(APPSTATE_PAUSEMENU);

    if (sym == SDLK_F1)
        CAppStateManager::SetActiveAppState(APPSTATE_GAMEOVER);
}

void CAppStateGame::OnActivate() {
    if((Surf_BG = CSurface::OnLoad("content/image/bg.jpg")) == NULL) {
        return;
    }

    if((Surf_Ball = CSurface::OnLoad("content/image/cube.jpg")) == NULL) {
        return;
    }

    if((Surf_TopBar = CSurface::OnLoad("content/image/top_bar.png")) == NULL) {
        return;
    }

    if((CubeBoom = CSoundBank::SoundControl.OnLoad("content/sounds/CubeBoom.wav")) == -1) {
    return;
    }

    Font = CFont::OnLoad("content/fonts/visitor2.ttf", 37);
    FontS = CFont::OnLoad("content/fonts/visitor2.ttf", 30);
    CField::Field.Score = 0;
    CField::Field.TMP_Score = 0;
    CField::Field.Buf_Score = 0;
    StartTime = 0;

    CField::Field.OnInit(640, 480);
}

void CAppStateGame::OnDeactivate() {
    SDL_FreeSurface(Surf_BG);
    SDL_FreeSurface(Surf_TopBar);
    SDL_FreeSurface(Surf_Ball);
}

void CAppStateGame::OnLoop() {
    if (CField::Field.Score != CField::Field.Buf_Score && SDL_GetTicks() - StartTime > 40) {
        CField::Field.Score += int(sqrt(CField::Field.Buf_Score / 2));
        if (CField::Field.Score > CField::Field.Buf_Score)
            CField::Field.Score = CField::Field.Buf_Score;
        StartTime = SDL_GetTicks();
    }

    if (CField::Field.Live == false) {
        CAppStateManager::SetActiveAppState(APPSTATE_GAMEOVER);
        return;
    }
    if (CField::Field.MotionX || CField::Field.MotionY) {
        CField::Field.OnLoop();
        return;
    }
    if (CField::Field.Animate) {
        CField::Field.OnAnimate();
        return;
    }
}

extern TTF_Font*    Font;

void CAppStateGame::OnRender(SDL_Surface* Surf_Display) {
    CSurface::OnDraw(Surf_Display, Surf_BG, 0, 0);
    CField::Field.OnRender(Surf_Display, Surf_Ball);
    CSurface::OnDraw(Surf_Display, Surf_TopBar, 0, 0);
    CFont::OnDraw(Font, 0, CField::Field.Score, Surf_Display, 105, 7);
    if (!CField::Field.SelectedBalls.empty())
        CFont::OnDraw(FontS, 1, CField::Field.TMP_Score, Surf_Display, CField::Field.SelectedBalls[0].x * 40 + 5, CField::Field.SelectedBalls[0].y * 40 + 45);
}

CAppStateGame* CAppStateGame::GetInstance() {
    return &Instance;
}
